<template>
  <div class="container-questions-new">
    <el-card class="box-card">
      <!-- 头部提示信息 -->
      <div slot="header" class="clearfix">
        <span>试题录入</span>
      </div>

      <!-- 表单区域 -->
      <div class="formBox">
        <el-form ref="form" :rules="rules" :model="form" label-width="85px">
          <el-form-item label="学科:" prop="subjectID">
            <el-select
              style="width: 35%"
              v-model="form.subjectID"
              placeholder="请选择"
              @change="hGetDirectorys"
            >
              <el-option
                v-for="item in SubjectsList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录:" prop="catalogID">
            <el-select
              style="width: 35%"
              v-model="form.catalogID"
              placeholder="请选择活动区域"
            >
              <el-option
                v-for="item in directorysList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="企业:" prop="enterpriseID">
            <el-select
              style="width: 35%"
              v-model="form.enterpriseID"
              placeholder="请选择"
            >
              <el-option
                v-for="item in companysList"
                :key="item.id"
                :label="item.company"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="城市:" prop="province">
            <el-select
              style="width: 17%; margin-right: 1%"
              v-model="form.province"
              placeholder="请选择"
              @change="cityFn"
            >
              <el-option
                v-for="item in provincesList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
            <el-select
              style="width: 17%"
              v-model="form.city"
              placeholder="请选择"
            >
              <el-option
                v-for="item in citysList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向:" prop="direction">
            <el-select
              style="width: 35%"
              v-model="form.direction"
              placeholder="请选择活动区域"
            >
              <el-option
                v-for="item in directionList"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="题型:" prop="questionType">
            <el-radio-group v-model="form.questionType">
              <el-radio
                v-for="item in questionTypeList"
                :key="item.value"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="难度:" prop="difficulty">
            <el-radio-group v-model="form.difficulty">
              <el-radio
                v-for="item in difficultyList"
                :key="item.value"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="题干:" prop="question">
            <template>
              <!-- 富文本子组件 -->
              <edit-or-one ref="sonOne" @change="changeFn"></edit-or-one>
              <!-- 需要隐藏的表单 -->
              <el-input
                style="visibility: hidden"
                v-model="form.question"
              ></el-input>
            </template>
          </el-form-item>
          <el-form-item
            label="选项:"
            v-if="form.questionType === 1 || form.questionType === 2"
          >
            <!-- 单选框显示隐藏 -->
            <template v-if="form.questionType === 1">
              <el-radio-group v-model="radio">
                <div
                  v-for="(item, index) in form.options"
                  :key="index"
                  class="selbox"
                >
                  <el-radio :label="item.code">
                    <template>{{ item.code }}:</template>
                  </el-radio>
                  <el-input
                    style="width: 300px; margin-right: 5px; margin-left: 5px"
                    v-model="item.title"
                  ></el-input>
                  <div class="updateBox">
                    <UploadImg v-model="item.img"></UploadImg>
                    <el-button icon="el-icon-close" circle></el-button>
                  </div>
                </div> </el-radio-group
            ></template>

            <!-- 多选框显示隐藏 -->
            <template v-if="form.questionType === 2">
              <el-checkbox-group v-model="check">
                <div
                  v-for="(item, index) in form.options"
                  :key="index"
                  class="selbox"
                >
                  <el-checkbox :label="item.code">
                    <template>{{ item.code }}: </template>
                  </el-checkbox>
                  <el-input
                    style="width: 300px; margin-right: 5px; margin-left: 5px"
                    v-model="item.title"
                  ></el-input>
                  <div class="updateBox">
                    <UploadImg v-model="item.img"></UploadImg>
                    <el-button icon="el-icon-close" circle></el-button>
                  </div>
                </div> </el-checkbox-group
            ></template>
          </el-form-item>
          <el-form-item
            v-if="form.questionType === 1 || form.questionType === 2"
          >
            <el-button
              :type="isadd"
              :disabled="form.questionType === 1"
              size="small"
              icon="el-icon-plus"
              @click="addFn"
              >增加选项与答案</el-button
            >
          </el-form-item>
          <el-form-item label="解析视频:" prop="videoURL">
            <el-input style="width: 35%" v-model="form.videoURL"></el-input>
          </el-form-item>

          <el-form-item label="答案解析:" prop="answer">
            <template>
              <!-- 富文本2 -->
              <edit-or-two ref="sonTwo" @change="changeFN"></edit-or-two>
              <!-- 需要隐藏的表单 -->
              <el-input
                v-model="form.answer"
                style="visibility: hidden"
              ></el-input>
            </template>
          </el-form-item>
          <el-form-item label="题目备注:">
            <el-input
              style="width: 35%"
              type="textarea"
              v-model="form.remarks"
            ></el-input>
          </el-form-item>
          <el-form-item  label="试题标签:">
            <el-select
              style="width: 35%"
              v-model="form.tags"
              multiple
              collapse-tags
              placeholder="请选择试题标签"
            >
              <el-option
                v-for="item in TagsimpleList"
                :key="item.value"
                :label="item.label"
                :value="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              :type="type"
              @click="onSubmit"
              v-text="comfirmbtn"
            ></el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
// 引入富文本子组件
import EditOrOne from '../components/editorone.vue'
import EditOrTwo from '../components/editortwo.vue'
import UploadImg from '../components/UploadImg.vue'

// 引入城市列表
import { provinces, citys } from '../../api/hmmm/citys'

// 引入方向列表
import { direction, questionType, difficulty } from '../../api/hmmm/constants'

// 引入组件获取学科列表
import { Subsimple as getSubjectsList } from '../../api/hmmm/subjects'

// 引入获取目录列表接口
import { Dirsimple as getdirectorys } from '../../api/hmmm/directorys'

// 引入获取企业列表接口
import { list as getCompanys } from '../../api/hmmm/companys'

// 引入获取标签列表接口
import { Tagsimple } from '../../api/hmmm/tags'

// 引入基础题库添加接口
import { add, Quedetail } from '../../api/hmmm/questions'
let i = 0
export default {
  components: {
    EditOrOne,
    EditOrTwo,
    UploadImg
  },
  data () {
    return {
      SubjectsList: [], // 学科列表
      directorysList: [], // 目录列表
      companysList: [], // 企业列表
      TagsimpleList: [], // 标签列表
      provincesList: provinces(), // 城市列表
      citysList: [], // 地区列表
      directionList: direction, // 方向列表
      questionTypeList: questionType, // 题型
      difficultyList: difficulty, // 难度
      radio: '', // 单选
      check: [], // 多选
      comfirmbtn: '确认提交', // 提交按钮
      type: 'primary', // 提交样式
      form: {
        subjectID: '', // 学科
        catalogID: '', // 目录
        enterpriseID: '', // 企业简称
        province: '', // 城市
        city: '', // 地区
        direction: '', // 方向
        questionType: '', // 题型
        difficulty: '', // 难度
        question: '', //  题干
        videoURL: '', // 解析视频
        answer: '', // 答案解析
        remarks: '', // 题目备注
        tags: '', // 试题标签
        options: [
          {
            code: 'A',
            img: '',
            title: '',
            isRight: '' // 单选
          },
          {
            code: 'B',
            img: '',
            title: '',
            isRight: '' // 单选
          },
          {
            code: 'C',
            img: '',
            title: '',
            isRight: '' // 单选
          },
          {
            code: 'D',
            img: '',
            title: '',
            isRight: '' // 单选
          }
        ] // 选择题数组
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'change' }
        ],
        catalogID: [
          { required: true, message: '请选择目录', trigger: 'change' }
        ],
        enterpriseID: [
          { required: true, message: '请选择企业', trigger: 'change' }
        ],
        province: [
          { required: true, message: '请选择城市', trigger: 'change' }
        ],
        city: [{ required: true, message: '请选择地区', trigger: 'change' }],
        direction: [
          { required: true, message: '请选择方向', trigger: 'change' }
        ],
        questionType: [
          { required: true, message: '请选择题型', trigger: 'change' }
        ],
        difficulty: [
          { required: true, message: '请选择难度', trigger: 'change' }
        ],
        question: [
          { required: true, message: '请输入题干', trigger: 'change' }
        ],
        answer: [
          { required: true, message: '请输入答案解析', trigger: 'change' }
        ]

      },
      isadd: 'danger', // 是否可以添加
      // 假数据
      ary: [
        'E',
        'F',
        'G',
        'H',
        'I',
        'G',
        'K',
        'L',
        'M',
        'N',
        'O',
        'P',
        'Q',
        'R',
        'S',
        'T',
        'U',
        'V',
        'W',
        'X',
        'Y',
        'Z'
      ]
    }
  },
  mounted () {
    // 掉方法获取学科列表
    this.hgetSubjectsList()

    // 掉方法获取企业列表
    this.getCompanysList()

    // 清除表单校验
    this.$refs.form.resetFields()

    // 掉方法判断是添加还是修改
    this.isUpDateOrAddFn()
  },
  methods: {
    // 点击提交触发的事件
    onSubmit () {
      this.$refs.form.validate(async (val) => {
        if (val) {
          this.form.tags =
            this.form.tags.length > 0 ? this.form.tags.join(',') : ''
          if (this.form.questionType === 1) {
            this.form.options.forEach((item) => {
              if (item.code === this.radio) {
                item.isRight = 1
              } else {
                item.isRight = 0
              }
            })
          } else if (this.form.questionType === 2) {
            this.form.options.forEach((item) => {
              if (this.check.includes(item.code)) {
                item.isRight = 1
              } else {
                item.isRight = 0
              }
            })
          }
          this.form.questionType += ''
          this.form.difficulty += ''
          // 掉接口提交数据
          await add(this.form)

          // 给出提示
          this.$message.success(
            this.$route.query.id ? '试题修改成功' : '试题录入成功'
          )

          // 跳转页面
          this.$router.push('/questions/list')
        }
      })
    },
    // 富文本1
    changeFn (msg) {
      this.form.question = msg.html
    },
    // 富文本2
    changeFN (msg) {
      this.form.answer = msg.html
    },
    // 点击增加多选题方法
    addFn () {
      if (this.ary[i]) {
        this.form.options.push({
          code: this.ary[i],
          img: '',
          title: '',
          isRight: '' // 单选
        })
        i++
      }
    },
    // 获取学科列表方法
    async hgetSubjectsList () {
      const res = await getSubjectsList()
      this.SubjectsList = res.data
    },
    // 监听学科获取目录和标签方法
    async hGetDirectorys () {
      const res = await getdirectorys({ subjectID: this.form.subjectID })
      this.directorysList = res.data

      const ret = await Tagsimple({ subjectID: this.form.subjectID })
      this.TagsimpleList = ret.data
    },
    // 获取企业列表接口
    async getCompanysList () {
      const res = await getCompanys({ pagesize: 10000 })

      this.companysList = res.data.items
    },
    // 通过城市获取地区
    cityFn () {
      this.citysList = citys(this.form.province)
    },
    // 判断是来自修改还是添加
    async isUpDateOrAddFn () {
      if (this.$route.query.id) {
        // 获取修改传递的id调接口
        const res = await Quedetail({ id: this.$route.query.id })

        this.comfirmbtn = '确认修改'
        this.type = 'success'
        res.data.questionType *= 1
        res.data.difficulty *= 1
        if (res.data.tags) {
          res.data.tags = res.data.tags.split(',')
        }
        if (res.data.questionType === 1) {
          res.data.options.forEach((item) => {
            if (item.isRight === 1) {
              this.radio = item.code
            }
          })
        } else if (res.data.questionType === 2) {
          res.data.options.forEach((item) => {
            if (item.isRight === 1) {
              this.check.push(item.code)
            }
          })
        }
        this.$refs.sonOne.$refs.editor.children[0].innerHTML =
          res.data.question
        this.$refs.sonTwo.$refs.editor.children[0].innerHTML = res.data.answer

        this.form = res.data

        // 监听学科获取目录和标签方法
        this.hGetDirectorys()

        // 获取地区
        this.cityFn()
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container-questions-new {
  padding: 20px 15px;
  .formBox {
    padding: 10px 10px 25px 30px;
  }
  .selbox {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 16px;
  color: #000;
  width: 100px;
  height: 55px;
  line-height: 55px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 55px;
  display: block;
  border-radius: 5px;
  padding-bottom: 2px;
  padding-right: 3px;
}
.updateBox {
  width: 100px;
  height: 55px;
  border-radius: 8px;
  position: relative;
}
::v-deep button.is-circle {
  position: absolute;
  top: -17px;
  right: -22px;
  transform: scale(0.5);
  border: 2px solid #888;
}
::v-deep .selbox label.el-radio {
  margin: 0;
}
</style>
